import {ActivatedRouteSnapshot, Resolve, Router} from '@angular/router';
import {Product} from '../product/product.component';
import {Injectable} from '@angular/core';

@Injectable() // 依赖注入
export class ProductResolve implements Resolve<Product> {
  constructor(private router: Router) {
  }

  // 忽略部分参数和省略返回值
  resolve(route: ActivatedRouteSnapshot) {
    const id = route.params['id'];

    if (id < 4) {
      return new Product(id, 'pen');
    } else {
      this.router.navigate(['/']);
    }
    return undefined;
  }

}
